unit QBOpersEditor;
{$I QBuilderVerCtrl.inc}
interface

uses
  {$IFDEF VCL16}
    Winapi.Windows,
    Winapi.Messages,
    System.SysUtils,
    System.Classes,
    Vcl.Graphics,
    Vcl.Controls,
    Vcl.Forms,
    Vcl.Dialogs,
    Vcl.StdCtrls,
  {$ELSE}
    Windows,
    Messages,
    SysUtils,
    Classes,
    Graphics,
    Controls,
    Forms,
    Dialogs,
    StdCtrls,
  {$ENDIF}
  QBWherePanel,
  QBParser;

type

  TQBEditRestrictions = set of (erAdd, erDelete, erName, erArgument);

  TQBOperatorsEditor = class(TForm)
    gbOperators: TGroupBox;
    gbFeatures: TGroupBox;
    buOK: TButton;
    buCancel: TButton;
    lbOperators: TListBox;
    laName: TLabel;
    edCaption: TEdit;
    laCaption: TLabel;
    laCount: TLabel;
    laSQLCaption: TLabel;
    laSQL: TLabel;
    buAdd: TButton;
    buDelete: TButton;
    edName: TEdit;
    edCount: TEdit;
    laPrefix: TLabel;
    laRelate: TLabel;
    laPostfix: TLabel;
    edPrefix: TEdit;
    edRelate: TEdit;
    edPostfix: TEdit;
    chCommaList: TCheckBox;
    procedure lbOperatorsClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure buOKClick(Sender: TObject);
    procedure buAddClick(Sender: TObject);
    procedure buDeleteClick(Sender: TObject);
    procedure ChangeData(Sender: TObject);
  private
    FOldIndex: Integer;
    FModified: Boolean;
    FRestrictions: TQBEditRestrictions;
    FSourceList: TQBOperators;
    procedure SetRestrictions(Value: TQBEditRestrictions);
  public
    property Restrictions: TQBEditRestrictions read FRestrictions write
      SetRestrictions;
    property SourceList: TQBOperators read FSourceList;
  end;

var
  QBOperatorsEditor: TQBOperatorsEditor;

implementation

{$R *.DFM}

procedure TQBOperatorsEditor.lbOperatorsClick(Sender: TObject);
var
  Op: TQBOperator;
begin
  if FOldIndex > -1 then begin
    Op := FSourceList.Items[FOldIndex];
    if Assigned(Op) and FModified then begin
      Op.Name := edName.Text;
      Op.Caption := edCaption.Text;
      Op.Relate := edRelate.Text;
      Op.Prefix := edPrefix.Text;
      Op.Postfix := edPostfix.Text;
      Op.CommaList := chCommaList.Checked;
      try
        Op.Count := StrToInt(edCount.Text) - 1;
      except
        Op.Count := 1;
      end;
    end;
  end;
  FOldIndex := lbOperators.ItemIndex;
  FModified := false;
  buDelete.Enabled := not (erDelete in FRestrictions) and (FOldIndex <> -1);
  gbFeatures.Visible := (FOldIndex <> -1);
  if FOldIndex = -1 then Exit;
  Op := FSourceList.Items[FOldIndex];
  edCount.Text := IntToStr(Op.Count + 1);
  edName.Text := Op.Name;
  edCaption.Text := Op.Caption;
  edPrefix.Text := Op.Prefix;
  edRelate.Text := Op.Relate;
  edPostfix.Text := Op.Postfix;
  chCommaList.Checked := Op.CommaList;
  ChangeData(nil);
end;

procedure TQBOperatorsEditor.FormCreate(Sender: TObject);
begin
  FSourceList := TQBOperators.Create(TQBOperator);
  FOldIndex := -1;
  FModified := false;
  FRestrictions := [];
end;

procedure TQBOperatorsEditor.FormShow(Sender: TObject);
var
  I: Integer;
  Op: TQBOperator;
begin
  lbOperators.Clear;
  for I := 0 to FSourceList.Count - 1 do begin
    Op := FSourceList.Items[I];
    lbOperators.Items.Add(Op.Name);
  end;
  if lbOperators.Items.Count > 0 then begin
    lbOperators.ItemIndex := 0;
    lbOperatorsClick(lbOperators);
  end;
end;

procedure TQBOperatorsEditor.FormDestroy(Sender: TObject);
begin
  FSourceList.Free();
end;

procedure TQBOperatorsEditor.buOKClick(Sender: TObject);
{var
  Op: TQBOperator;}
begin
  lbOperatorsClick(lbOperators);
{  if FOldIndex <> -1 then begin
    Op := FSourceList.Items[FOldIndex];
    if (Op <> nil) and (Op.Caption <> edCaption.Text) then begin
      Op.Caption := edCaption.Text;
    end;
  end;}
end;

procedure TQBOperatorsEditor.buAddClick(Sender: TObject);
var
  Op: TQBOperator;
begin
  Op := FSourceList.Add();
  lbOperators.ItemIndex := lbOperators.Items.Add(Op.Name);
  lbOperatorsClick(Self);
end;

procedure TQBOperatorsEditor.buDeleteClick(Sender: TObject);
begin
  if FOldIndex = -1 then Exit;
  FSourceList.Delete(FOldIndex);
  lbOperators.Items.Delete(FOldIndex);
  FOldIndex := lbOperators.ItemIndex;
  buDelete.Enabled := not (erDelete in FRestrictions) and (FOldIndex <> -1);
  gbFeatures.Visible := (FOldIndex <> -1);
end;

procedure TQBOperatorsEditor.ChangeData(Sender: TObject);
var
  Ind, Cnt: Integer;
begin
  if (Sender is TEdit) and (TEdit(Sender).Focused)
    then FModified := True;
  if Sender = chCommaList
    then FModified := True;
  if Sender = edName then lbOperators.Items.Strings[FOldIndex] := edName.Text;
//Update SQL Expression
  laSQL.Caption := '??? ' + edName.Text + edPrefix.Text;
  try Cnt := StrToInt(edCount.Text) - 2;
  except
    Cnt := 0;
    edCount.OnChange := nil;
    edCount.Text := '2';
    edCount.OnChange := ChangeData;
  end;
  for Ind := 0 to Cnt do
  begin
    laSQL.Caption := laSQL.Caption + ' ??? ';
    if Ind < Cnt then laSQL.Caption := laSQL.Caption + edRelate.Text;
  end;
  laSQL.Caption := laSQL.Caption + edPostfix.Text;
end;

procedure TQBOperatorsEditor.SetRestrictions(
  Value: TQBEditRestrictions);
begin
  buAdd.Enabled := not (erAdd in Value);
  buDelete.Enabled := not (erDelete in Value);
  edName.Enabled := not ((erName in Value) or (erAdd in Value));
  if erArgument in Value then begin
    edCount.Enabled := false;
    edPrefix.Enabled := false;
    edRelate.Enabled := false;
    edPostfix.Enabled := false;
  end else begin
    edCount.Enabled := true;
    edPrefix.Enabled := true;
    edRelate.Enabled := true;
    edPostfix.Enabled := true;
  end;
  FRestrictions := Value;
end;

end.
